package com.example.jf.jfapp.widget.bottomtab;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by weidong on 2017/7/7.
 */

public class TabView extends LinearLayout{

    //选中
    private int choooseResId;
    //未选中
    private int normalResId;
    private ImageView icon;
    private TextView title;

    String mTitleStr;

    public TabView(Context context,String title,int choooseResId,int normalResId) {
        super(context);
        this.choooseResId = choooseResId;
        this.normalResId = normalResId;
        this.mTitleStr = title;
        init();
    }

    private void init(){
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);

        icon = new ImageView(getContext());

        //图标的属性
        LayoutParams iconParams = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT
                , ViewGroup.LayoutParams.WRAP_CONTENT);
        icon.setLayoutParams(iconParams);

        icon.setImageResource(normalResId);

        //添加图标
        addView(icon);

        //标题
        title = new TextView(getContext());

        LayoutParams titleParams = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT
                , ViewGroup.LayoutParams.WRAP_CONTENT);
        title.setLayoutParams(titleParams);

        title.setText(mTitleStr);

        addView(title);
    }

    @Override
    public void setSelected(boolean selected) {
        if(selected){
            //选中
            if(icon!=null){
                icon.setImageResource(choooseResId);
            }
            if(title!=null){
                title.setTextColor(0xffffc107);
            }

        }else{
            if(icon!=null){
                icon.setImageResource(normalResId);
            }

            if(title!=null){
                title.setTextColor(Color.GRAY);
            }
        }
    }
}
